// 屏幕适配管理
import { _decorator, Component, view, screen, ResolutionPolicy, Node } from 'cc'
// import { BgScale } from './BgScale'
const { ccclass, property } = _decorator

@ccclass('AdaptationManager')
export class AdaptationManager extends Component {
  @property(Node)
  bg: Node = null
  start() {
    this.checkRatio()
  }

  checkRatio() {
    const win = screen.windowSize
    const design = view.getDesignResolutionSize()
    // 设计分辨率
    const designResolutionRatio = design.height / design.width
    // 屏幕分辨率
    const windowResolutionRatio = win.height / win.width
    // 屏幕高于设计尺寸，采用宽度适配
    if (windowResolutionRatio > designResolutionRatio) {
      view.setDesignResolutionSize(
        design.width,
        design.height,
        ResolutionPolicy.FIXED_WIDTH,
      )
      const scaleX = win.width / design.width
      const scale = win.height / (design.height * scaleX)
      console.log('this.bg', this.bg)
      this.bg.setScale(scale, scale)
    } else {
      // 屏幕低于设计尺寸，采用高度适配
      view.setDesignResolutionSize(
        design.width,
        design.height,
        ResolutionPolicy.FIXED_HEIGHT,
      )
      const scaleY = win.height / design.height
      const scale = win.width / (design.width * scaleY)
      this.bg.setScale(scale, scale)
      console.log('this.bg', this.bg)
    }
  }
}
